/**
 * 
 */
package com.swim.settlementapply.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.swim.settlementapply.entity.SettlementApply;
import com.swim.settlementapply.entity.SettlementApplyCount;
import com.swim.settlementapply.entity.SettlementApplyItem;
import com.swim.settlementapply.service.SettlementApplyItemService;
import com.swim.settlementapply.service.SettlementApplyService;
import com.swim.settlementrecord.entity.SettlementRecord;

/**
 * 结算申请Controller
 * @author 刘军武
 * @version 2021-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/settlementapply/settlementApply")
public class SettlementApplyController extends BaseController {

	@Autowired
	private SettlementApplyService settlementApplyService;
	
	@Autowired
	private SettlementApplyItemService settlementApplyItemService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public SettlementApply get(String id, boolean isNewRecord) {
		return settlementApplyService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("settlementapply:settlementApply:view")
	@RequestMapping(value = {"list", ""})
	public String list(SettlementApply settlementApply, Model model) {
		model.addAttribute("settlementApply", settlementApply);
		return "swim/settlementapply/settlementApplyList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("settlementapply:settlementApply:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SettlementApply> listData(SettlementApply settlementApply, HttpServletRequest request, HttpServletResponse response) {
		Page<SettlementApply> page = settlementApplyService.findPage(new Page<SettlementApply>(request, response), settlementApply); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("settlementapply:settlementApply:view")
	@RequestMapping(value = "form")
	public String form(SettlementApply settlementApply, Model model) {
		model.addAttribute("settlementApply", settlementApply);
		return "swim/settlementapply/settlementApplyForm";
	}

	/**
	 * 保存结算申请
	 */
	@RequiresPermissions("settlementapply:settlementApply:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SettlementApply settlementApply) {
		settlementApplyService.save(settlementApply);
		return renderResult(Global.TRUE, text("保存结算申请成功！"));
	}

	/**
	 * 查看结算审核表单
	 */
	@RequestMapping(value = "settlementCheck")
	public String settlementCheck(SettlementRecord settlementRecord, Model model) {
		settlementRecord.setSubmitter(UserUtils.getUser().getUserName()); // 提交人
		model.addAttribute("settlementRecord", settlementRecord);
		return "swim/settlementrecord/settlementRecordForm";
	}
	
	/**
	 * 查询结算申请明细列表数据
	 */
	@RequestMapping(value = "settlementApplyItemListData")
	@ResponseBody
	public Page<SettlementApplyItem> settlementApplyItemListData(SettlementApplyItem settlementApplyItem, HttpServletRequest request, HttpServletResponse response) {
		Page<SettlementApplyItem> page = settlementApplyItemService.findPage(new Page<SettlementApplyItem>(request, response), settlementApplyItem); 
		return page;
	}

	/**
	 * 跳转申请结算页
	 */
	@RequestMapping(value = "settlementApplyCount")
	public String settlementApplyCount(Model model) {
		//获取当前申请结算统计数据
		SettlementApplyCount settlementApplyCount = settlementApplyService.getSettlementApplyCount();
		model.addAttribute("settlementApplyCount", settlementApplyCount);
		return "swim/settlementapply/settlementApplyCount";
	}
	
	/**
	 * 跳转查看详情页
	 */
	@RequestMapping(value = "details")
	public String details(SettlementApply settlementApply, Model model) {
		model.addAttribute("settlementApply", settlementApply);
		return "swim/settlementapply/settlementApplyDetails";
	}
	
	/**
	 * 申请结算
	 */
	@RequiresPermissions("natatoriumorder:natatoriumOrder:edit")
	@RequestMapping(value = "settlementApply")
	@ResponseBody
	public String settlementApply() {
		settlementApplyService.settlementApply();
		return renderResult(Global.TRUE, text("申请结算成功！"));
	}
}